$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'ypgl/yaopinxinxi/list',
        datatype: "json",
        colModel: [
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true },
            { label: '药品名称', name: 'yaopinming', index: 'yaopinming', width: 80 },
			{ label: '企业名称', name: 'qiyemingcheng', index: 'qiyemingcheng', width: 80 },
            { label: '生产地址', name: 'shengchandizhi', index: 'shengchandizhi', width: 80 },
            { label: '电话', name: 'dianhua', index: 'dianhua', width: 80 },
			{ label: '生产日期', name: 'shengchanriqi', index: 'shengchanriqi', width: 80 },
			{ label: '药品分类', name: 'yaopinfenlei', index: 'yaopinfenlei', width: 80 }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });


    /*$('#shengchanriqi').datepicker({
        format: 'yyyy-mm-dd',
    });*/
    $("#shengchanriqi").datetimepicker({
        dateFormat:'yy-mm-dd',
        timeFormat: "HH:mm:ss"
    });
});



var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		yaopinxinxi: {},
        q:{
            dianhua:null,
            qiyemingcheng:null,
            shengchandizhi:null,
            shengchanriqi:null,
            yaopinfenlei:null,
            yaopinming:null,
        }
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.yaopinxinxi = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
            vm.yaopinxinxi.shengchanriqi=$("#shengchanriqi").val();
		    //$('#btnSaveOrUpdate').button('loading').delay(1000).queue(function() {
                var url = vm.yaopinxinxi.id == null ? "ypgl/yaopinxinxi/save" : "ypgl/yaopinxinxi/update";
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.yaopinxinxi),
                    success: function(r){
                        if(r.code === 0){
                             layer.msg("操作成功", {icon: 1});
                             vm.reload();
                             $('#btnSaveOrUpdate').button('reset');
                             $('#btnSaveOrUpdate').dequeue();
                        }else{
                            layer.alert(r.msg);
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        }
                    }
                });
			//});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			var lock = false;
            layer.confirm('确定要删除选中的记录？', {
                btn: ['确定','取消'] //按钮
            }, function(){
               if(!lock) {
                    lock = true;
		            $.ajax({
                        type: "POST",
                        url: baseURL + "ypgl/yaopinxinxi/delete",
                        contentType: "application/json",
                        data: JSON.stringify(ids),
                        success: function(r){
                            if(r.code == 0){
                                layer.msg("操作成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                            }else{
                                layer.alert(r.msg);
                            }
                        }
				    });
			    }
             }, function(){
             });
		},
		getInfo: function(id){
			$.get(baseURL + "ypgl/yaopinxinxi/info/"+id, function(r){
                vm.yaopinxinxi = r.yaopinxinxi;
            });
		},
		reload: function (event) {
			vm.showList = true;
            var shengchanriqi=$("#shengchanriqi").val();
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
			    postData:{
                    dianhua:vm.q.dianhua,
                    qiyemingcheng:vm.q.qiyemingcheng,
                    shengchandizhi:vm.q.shengchandizhi,
                    shengchanriqi:vm.q.shengchanriqi,
                    yaopinfenlei:vm.q.yaopinfenlei,
                    yaopinming:vm.q.yaopinming,
                },
                page:page
            }).trigger("reloadGrid");
		}
	}
});
